//
//  XS_KCHour.swift
//  XueDao
//
//  Created by 韩云智 on 2021/1/10.
//

import UIKit
import RxCocoa
import RxSwift
import RxDataSources

class XS_KCHour: UIView {
    lazy var disposeBag = DisposeBag()
    lazy var modelPS = PublishSubject<[XS_KCplayVideohourListM]>()
    lazy var actionPS = PublishSubject<XS_KCplayVideohourListM>()
    lazy var isBuy = ""
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        upView()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func upView() {
        backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: .scale*150, height: .scale*74)
        flowLayout.scrollDirection = .horizontal
        let collection = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
        collection.showsHorizontalScrollIndicator = false
        collection.layer.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        collection.layer.cornerRadius = .scale*4
        collection.bounces = false
        addSubview(collection)
        collection.snp.makeConstraints {
            $0.left.top.equalTo(.scale*16)
            $0.right.equalTo(-.scale*16)
            $0.height.equalTo(.scale*74)
            $0.bottom.lessThanOrEqualTo(-.scale*16)
        }
        
        collection.register(XS_KCHourCell.self, forCellWithReuseIdentifier: "cell")
        let dataSource = RxCollectionViewSectionedReloadDataSource<SectionModel<String, XS_KCplayVideohourListM>>(configureCell: { [weak self] ds, cv, ip, it in
            let cell = cv.dequeueReusableCell(withReuseIdentifier: "cell", for: ip) as! XS_KCHourCell
            cell.modelPS.onNext((it, self?.isBuy ?? ""))
            return cell
        })
        
        modelPS.asDriver(onErrorJustReturn: [])
            .flatMapLatest({ Driver.just([SectionModel<String, XS_KCplayVideohourListM>(model: "", items: $0)]) })
            .drive(collection.rx.items(dataSource: dataSource))
            .disposed(by: disposeBag)
        collection.rx.modelSelected(XS_KCplayVideohourListM.self)
            .asDriver()
            .drive(onNext: { [weak self] model in
                self?.actionPS.onNext(model)
            }).disposed(by: disposeBag)
    }
}

class XS_KCHourCell: XS_BaseCCell {
    lazy var modelPS = PublishSubject<(XS_KCplayVideohourListM, String)>()
    override func upView() {
        layer.borderWidth = 1
        layer.cornerRadius = .scale*4
        layer.masksToBounds = true
        
        let shapeLayer = CAShapeLayer()
        shapeLayer.fillColor = #colorLiteral(red: 0.1647058824, green: 0.7411764706, blue: 0.6117647059, alpha: 0.1)
        shapeLayer.path = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 0, height: 74) * .scale).cgPath
        layer.addSublayer(shapeLayer)
        
        let nameLab = UILabel.xsLabel(size: .scale*14, fontName: .medium, lines: 2)
        contentView.addSubview(nameLab)
        nameLab.snp.makeConstraints({
            $0.top.equalTo(.scale*8)
            $0.left.equalTo(.scale*12)
            $0.right.lessThanOrEqualTo(-.scale*12)
        })
        let tagLab = UILabel.xsLabel(size: .scale*12, fontName: .regular)
        contentView.addSubview(tagLab)
        tagLab.snp.makeConstraints({
            $0.bottom.equalTo(-.scale*8)
            $0.left.equalTo(.scale*12)
            $0.right.lessThanOrEqualTo(-.scale*12)
        })
        
//        NotificationCenter.default.post(name: Notification.Name("XS_VideoProgress"), object: CGFloat(0.5))
        NotificationCenter.default.rx
            .notification(Notification.Name("XS_VideoProgress"))
            .subscribe(onNext: { noti in
                guard let object = noti.object as? CGFloat else { return }
                shapeLayer.path = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 150 * object, height: 74) * .scale).cgPath
            }).disposed(by: disposeBag)
        
        let lock = UIImageView(image: UIImage(named: "icon_24px_播放_1"))
        contentView.addSubview(lock)
        lock.snp.makeConstraints({
            $0.width.height.equalTo(.scale*20)
            $0.bottom.equalTo(-.scale*8)
            $0.right.equalTo(-.scale*12)
        })
        
        modelPS.subscribe(onNext: { [weak self] item, isBuy in
            guard let `self` = self else { return }
            if item.isCurrent == "yes" {
                self.layer.borderColor = #colorLiteral(red: 0.1647058824, green: 0.7411764706, blue: 0.6117647059, alpha: 1)
                nameLab.textColor = #colorLiteral(red: 0.1647058824, green: 0.7411764706, blue: 0.6117647059, alpha: 1)
                tagLab.textColor = #colorLiteral(red: 0.1647058824, green: 0.7411764706, blue: 0.6117647059, alpha: 1)
                shapeLayer.opacity = 1
            } else {
                self.layer.borderColor = #colorLiteral(red: 0.9019607843, green: 0.9137254902, blue: 0.9450980392, alpha: 1)
                nameLab.textColor = #colorLiteral(red: 0.2196078431, green: 0.2352941176, blue: 0.2470588235, alpha: 1)
                tagLab.textColor = #colorLiteral(red: 0.6666666667, green: 0.6941176471, blue: 0.7176470588, alpha: 1)
                shapeLayer.opacity = 0
            }
            lock.isHidden = (item.isWatch == "yes") || (isBuy == "yes")
            nameLab.text = item.curriculumHourTitle
            tagLab.text = item.duration // + "分钟"
        }).disposed(by: disposeBag)
    }
}
